/**
 * xx时间以前
 */
function timeAgo(date) {
  if (date != +date) return '';

  if (typeof date !== 'object') {
    date = new Date(date*1000);
  }

  var seconds = Math.floor((new Date() - date) / 1000);
  var intervalType;

  var interval = Math.floor(seconds / 31536000);

  if (interval >= 1) {
    intervalType = '年';
  } else {
    interval = Math.floor(seconds / 2592000);
    if (interval >= 1) {
      intervalType = '月';
    } else {
      interval = Math.floor(seconds / 86400);
      if (interval >= 1) {
        intervalType = '天';
      } else {
        interval = Math.floor(seconds / 3600);
        if (interval >= 1) {
          intervalType = '小时';
        } else {
          interval = Math.floor(seconds / 60);
          if (interval >= 1) {
            intervalType = '分钟';
          } else {
            interval = seconds;
            intervalType = '秒';
          }
        }
      }
    }
  }

  return intervalType === '秒'? '刚刚' : interval + intervalType + '前';
}

/**
 * 数字格式转换
 */
function numFormat(number) {
  if (number != +number) return '';

  let sy = Math.pow(10, 9);
  let y = Math.pow(10, 8);
  let qw = Math.pow(10, 7);
  let sw = Math.pow(10, 5);
  let w = Math.pow(10, 4);
  let q = Math.pow(10, 3);
  let flag = '', value;

  if (number - sy >= 0) {
      // >=10亿，向下取整
    flag = '亿';
    value = Math.floor(number / y);
  } else if (number - y >= 0) {
      // 1亿=< 且 <10亿,取一位小数向下取整
    flag = '亿';
    value = (Number(Math.floor(number / qw) / 10).toFixed(1) + '').replace(/\.0$/, '');
  } else {
    if (number - sw > 0) {
          // 10万=< 且 <1亿，向下取整
      flag = '万';
      value = Math.floor(number / w);
    } else if (number - w >= 0) {
          // 1.1万=< 且 <10万取一位小数向下取整
      flag = '万';
      value = (Number(Math.floor(number / q) / 10).toFixed(1) + '').replace(/\.0$/, '');
    } else {
          // <1万，原样输出
      value = number;
    }
  }

  return value + flag;
}

// 视频时长格式转换
function durationFormat(duration) {
  if (duration != +duration) return '';

  let res = [],
    tmp;

  if (duration / 3600 >= 1) {
    tmp = Math.floor(duration / 3600);
    duration -= tmp * 3600;
    res.push(tmp);
  }

  if (duration / 60 >= 1) {
    tmp = Math.floor(duration / 60);
    duration -= tmp * 60;
    if (tmp < 10) tmp = '0' + tmp;
    res.push(tmp);
  } else {
    res.push('00');
  }

  if (duration < 10) duration = '0' + duration;
  res.push(duration);

  return res.join(':');
}

function elOffset(elem) {
  var left = 0,
    top = 0,
    offsetParent = elem;

  while (offsetParent != null && offsetParent != document.body) {
    left += offsetParent.offsetLeft;
    top += offsetParent.offsetTop;

    offsetParent = offsetParent.offsetParent;
  }

  return { left: left, top: top};
}

function getWinSize() {
  if (window.innerHeight && window.innerWidth) {
    return {
      winWidth: window.innerWidth,
      winHeight: window.innerHeight
    }
  }
  if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth) {
    return {
      winWidth: document.documentElement.clientWidth,
      winHeight: document.documentElement.clientHeight
    }
  }
}

function getScrollTop(element) {
  if (element === window) {
    return Math.max(window.pageYOffset || 0, document.documentElement.scrollTop);
  }

  return element.scrollTop;
}

function addClass(elem, cls) {
  if (!hasClass(elem, cls)) {
    elem.className = elem.className === '' ? cls : elem.className + ' ' + cls
  }
}

function removeClass(elem, cls) {
  if (hasClass(elem, cls)) {
    var newClass = ' ' + elem.className.replace(/[\t\r\n]/g, '') + ' '
    while (newClass.indexOf(' ' + cls + ' ') >= 0) {
      newClass = newClass.replace(' ' + cls + ' ', ' ')
    }
    elem.className = newClass.replace(/^\s+|\s+$/g, '')
  }
}

function hasClass(elem, cls) {
  cls = cls || ''
  if (cls.replace(/\s/g, '').length === 0) return false
  return new RegExp(' ' + cls + ' ').test(' ' + elem.className + ' ')
}

function loadScript(url, resolve, reject) {
  var script = document.createElement('script');
  script.src = url;
  script.crossOrigin = 'anonymous';
  script.onload = function () {
    resolve && resolve.call();
  };
  script.onerror = function () {
    reject && reject.call();
  };
  document.body.appendChild(script);
}

function addWinLoadEvent(func) {
  let oldOnLoad = window.onload;
  if (typeof window.onload !== 'function') {
    window.onload = func;
  } else {
    window.onload = function () {
      oldOnLoad();
      func();
    }
  }
}

export  { // eslint-disable-line no-undef
  timeAgo,
  numFormat,
  durationFormat,
  elOffset,
  getWinSize,
  getScrollTop,
  addClass,
  removeClass,
  hasClass,
  loadScript,
  addWinLoadEvent
};



// WEBPACK FOOTER //
// ./~/byted-toutiao-pc-business-components/src/js/utils.js
